/*
 *File name: hbox.c
 */

#include <gtk/gtk.h>
#include <glib.h>

extern int fanOnVal;
extern int fanOffVal;
extern int fanIntervalVal;

/*-- This function allows the program to exit properly when the window is closed --*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{
  g_print ("Quitting...\n");
  gtk_main_quit();
  return (FALSE);
}

/*-- This function responds to the mouse click on the button --*/
void button_clicked(GtkWidget *widget, gpointer gdata)
{
  g_print("Button was clicked.\n");
}

void button_DumpConfigFile(GtkWidget *widget, gpointer gdata)
{
	g_print("dumpConfig Button\n");
	openConfig();
}

int main (int argc, char *argv[])
{
  /*-- Declare the GTK Widgets used in the program --*/
  GtkWidget *window;
  GtkWidget *dumpButton;
  GtkWidget *hbox;
  GtkWidget *fanOffLabel;
  GtkWidget *fanOffSpinner;
  GtkWidget *fanOffAdjustment;
  GtkWidget *fanOnLabel;
  GtkWidget *fanOnSpinner;
  GtkWidget *fanOnAdjustment;
  GtkWidget *intervalLabel;
  GtkWidget *intervalSpinner;
  GtkWidget *intervalAdjustment;

  /*--  Initialize GTK --*/
  gtk_init (&argc, &argv);

  /*-- Create the new window --*/
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  /*-- Create the labels --*/
  openConfig();

  fanOffLabel = gtk_label_new("Off Temp (C):");
  fanOffAdjustment = gtk_adjustment_new((float)fanOffVal, 40.0, 99.0, 1.0, 5.0, 0.0);

  fanOnLabel = gtk_label_new("On Temp (C):");
  fanOnAdjustment = gtk_adjustment_new((float)fanOnVal, 40.0, 99.0, 1.0, 5.0, 0.0);

  intervalLabel = gtk_label_new("Poll Interval:");
  intervalAdjustment = gtk_adjustment_new((float)fanIntervalVal, 1.0, 30.0, 1.0, 5.0, 0.0);
  
  /*-- Create some buttons to fill the vbox with --*/
  dumpButton = gtk_button_new_with_label("show config file");
 
  fanOffSpinner = gtk_spin_button_new(fanOffAdjustment, 0, 0);
  gtk_spin_button_set_wrap(fanOffSpinner, FALSE);

  fanOnSpinner = gtk_spin_button_new(fanOnAdjustment, 0, 0);
  gtk_spin_button_set_wrap(fanOnSpinner, FALSE);
  
  intervalSpinner = gtk_spin_button_new(intervalAdjustment, 0, 0);
  gtk_spin_button_set_wrap(intervalSpinner, FALSE);

  /*-- Create the hbox --*/
  hbox = gtk_hbox_new(FALSE,0);
  /*-- Connect the window to the destroyapp function  --*/
  gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL);

  /*-- Connect all the buttons to the button_was_clicked function --*/
  gtk_signal_connect(GTK_OBJECT(dumpButton), "clicked", GTK_SIGNAL_FUNC(button_DumpConfigFile), NULL);

  /*-- Add all the buttons to the vbox --*/
  gtk_box_pack_start(GTK_BOX(hbox), dumpButton, FALSE, FALSE, 2);

  gtk_box_pack_start(GTK_BOX(hbox), fanOffLabel, FALSE, FALSE, 2);
  gtk_box_pack_start(GTK_BOX(hbox), fanOffSpinner, FALSE, FALSE, 2);
  
  gtk_box_pack_start(GTK_BOX(hbox), fanOnLabel, FALSE, FALSE, 2);
  gtk_box_pack_start(GTK_BOX(hbox), fanOnSpinner, FALSE, FALSE, 2);
   
  gtk_box_pack_start(GTK_BOX(hbox), intervalLabel, FALSE, FALSE, 2);
  gtk_box_pack_start(GTK_BOX(hbox), intervalSpinner, FALSE, FALSE, 2);
  
  /*-- Add the button to the window --*/
  gtk_container_add(GTK_CONTAINER (window), hbox);

  /*-- Add a border to the window to give the buttons a little room --*/
  gtk_container_border_width (GTK_CONTAINER (window), 15);

  /*-- Display the widgets --*/
  gtk_widget_show(hbox);
  //gtk_widget_show(dumpButton);
  gtk_widget_show(fanOffLabel);
  gtk_widget_show(fanOffSpinner);
  gtk_widget_show(fanOnLabel);
  gtk_widget_show(fanOnSpinner);
  gtk_widget_show(intervalLabel);
  gtk_widget_show(intervalSpinner);
  gtk_widget_show(window);

  /*-- Start the GTK event loop --*/
  gtk_main();

  /*-- Return 0 if exit is successful --*/
  return 0;
}
  
